package org.example.bookstore.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.bookstore.custom.ResultVO;
import org.example.bookstore.pojo.DO.User;
import org.example.bookstore.pojo.DTO.UpdatePasswordDTO;
import org.example.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Tag(name = "用户Api")
@RestController
@CrossOrigin
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * 用户登录 邮箱账号＋密码
     * */
    @Operation(summary = "账号密码登录")
    @PostMapping ("/login")
    public ResultVO<?> login(@Parameter(name = "email", description = "邮箱账号") @RequestParam  String email,
                             @Parameter(name = "password", description = "密码")  @RequestParam String password){
        return userService.login(email,password);
    }

    /*
     * 用户登录 邮箱账号＋密码
     * */
    @Operation(summary = "邮箱验证码登录")
    @PostMapping ("/loginByCode")
    public ResultVO<?> login2(@Parameter(name = "email", description = "邮箱账号") @RequestParam  String email,
                             @Parameter(name = "verificationCode", description = "验证码") @RequestParam String verificationCode){
        return userService.loginByCode(email,verificationCode);
    }



    /*
     * 用户注册
     * */
    @Operation(summary = "注册")
    @PostMapping("/register")
    public ResultVO<String> insertUser(@Parameter(name = "email", description = "邮箱账号") @RequestParam String email,
                                       @Parameter(name = "password", description = "密码") @RequestParam String password,
                                       @Parameter(name = "verificationCode", description = "验证码") @RequestParam String verificationCode){
        // 参数校验
        return ResultVO.success("用户注册信息",userService.insertUser(email,password,verificationCode));
    }

    /*
     * 用户个人信息修改
     * */
    @Operation(summary = "个人信息修改")
    @PutMapping("/updateUser")
    public ResultVO<String> updateUser(@RequestBody  User user){
        boolean is=userService.updateUser(user);
        if (is){
            return ResultVO.success("信息修改成功");
        }
        return ResultVO.error("修改失败");
    }
    /*
     * 获取用户个人信息
     * */
    @Operation(summary = "根据id获取用户个人信息")
    @GetMapping("/userinfo")
    public ResultVO<?> getUserInfo(@Parameter(name = "id", description = "用户id") @RequestParam Integer id){
        return ResultVO.success("获取成功",userService.selectById(id));
    }
    /*
     * 用户密码修改
     * */
    @Operation(summary = "用户密码修改")
    @PatchMapping("/updatepwd")
    public ResultVO<?> updatePasswordById(@Parameter(name = "updatePasswordDTO", description = "密码更新") @RequestBody UpdatePasswordDTO updatePasswordDTO){
        return userService.updatePassword(updatePasswordDTO.getId(), updatePasswordDTO.getOld_pwd() ,updatePasswordDTO.getNew_pwd());
    }
    /*
     * 头像上传存储到阿里云后 返回路径存到数据库
     * */
    @Operation(summary = "用户头像更新")
    @GetMapping ("/upavatar")
    public ResultVO<?> updateAvatarById (@Parameter(name = "id", description = "用户id") @RequestParam Integer id,
                                         @Parameter(name = "avatar", description = "头像路径") @RequestParam String avatar) {
        return userService.updateAvatar(id,avatar);
    }
}
